package com.yunkaweilai.android.activity.web;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.yunkaweilai.android.R;
import com.yunkaweilai.android.base.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * web界面
 * Created by Administrator on 2017/8/3.
 */

public class WebFullActivity extends BaseActivity {

    @BindView(R.id.id_webView)
    WebView idWebView;
    public static final String WEB_URL = "WEB_URL";

    WebSettings mWebSettings;
    private String url = "";

    public static void actionStart(Context context,String url){
        Intent intent = new Intent(context,WebFullActivity.class);
        intent.putExtra(WEB_URL,url);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_full);
        ButterKnife.bind(this);
        url = getIntent().getStringExtra(WEB_URL);
        if (!url.contains("http")){
            url = "http://m.hfwjx.com";
        }
        mWebSettings = idWebView.getSettings();
        mWebSettings.setJavaScriptEnabled(true);
        mWebSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        idWebView.addJavascriptInterface(new JSInterface(),"Order");
        idWebView.loadUrl(url);

        //设置不用系统浏览器打开,直接显示在当前Webview
        idWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        //设置WebChromeClient类
        idWebView.setWebChromeClient(new WebChromeClient() {


            //获取网站标题
            @Override
            public void onReceivedTitle(WebView view, String title) {
            }


            //获取加载进度
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress < 100) {
                    String progress = newProgress + "%";
                } else if (newProgress == 100) {
                    String progress = newProgress + "%";
                }
            }
        });

        //设置WebViewClient类
        idWebView.setWebViewClient(new WebViewClient() {
            //设置加载前的函数
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {

            }

            //设置结束加载函数
            @Override
            public void onPageFinished(WebView view, String url) {

            }
        });
    }


    //点击返回上一页面而不是退出浏览器
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK && idWebView.canGoBack()) {
//            idWebView.goBack();
//            return true;
//        }
        finish();
        return super.onKeyDown(keyCode, event);
    }

    //销毁Webview
    @Override
    protected void onDestroy() {
        if (idWebView != null) {
            idWebView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            idWebView.clearHistory();
            ((ViewGroup) idWebView.getParent()).removeView(idWebView);
            idWebView.destroy();
            idWebView = null;
        }
        super.onDestroy();
    }


    private class JSInterface{
        @JavascriptInterface
        public void CloseActivity(){//推出
            finish();
        }

        @JavascriptInterface
        public void OrderRefund(){//退款成功
            showToast("退款成功");
            finish();
        }
    }

}
